package com.cloudpivot.receivales.excel.controller;

import com.cloudpivot.receivales.excel.domain.ImportExcelDTO;
import com.cloudpivot.receivales.excel.service.impl.SalePlanExcelServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;

/**
 * 销售计划Excel导出Controller
 */
@Controller
@Slf4j
@RequestMapping("/excel")
public class SalePlanExcelController {

    @Autowired
    private SalePlanExcelServiceImpl salePlanExcelService;

    /**
     * 导出销售计划统计数据为Excel
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate 结束日期 (格式: yyyy-MM-dd)
     * @param response HTTP响应对象
     * @throws IOException IO异常
     */
    @GetMapping("/exportSalePlan")
    public void exportSalePlan(
            @RequestParam String startDate,
            @RequestParam String endDate,
            HttpServletResponse response) throws IOException {

        // 参数验证
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
            log.error("日期参数不能为空");
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }

        log.info("开始导出数据，开始日期: {}, 结束日期: {}", startDate, endDate);

        // 获取数据
        Map<String, List<ImportExcelDTO>> data = salePlanExcelService.handleAllData(startDate, endDate);

        // 生成Excel
        Workbook workbook = createExcel(data);

        // 设置响应头
        String fileName = "销售计划统计_" + startDate + "_" + endDate + ".xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

        // 输出Excel文件
        OutputStream out = response.getOutputStream();
        workbook.write(out);
        out.close();

        log.info("Excel导出完成");
    }

    /**
     * 创建Excel工作簿
     *
     * @param data 数据Map，key为日期，value为部门统计列表
     * @return Workbook对象
     */
    private Workbook createExcel(Map<String, List<ImportExcelDTO>> data) {
        Workbook workbook = new XSSFWorkbook();
        CellStyle headerStyle = createHeaderStyle(workbook);
        CellStyle dataStyle = createDataStyle(workbook);
        CellStyle summaryStyle = createSummaryStyle(workbook);
        CellStyle dateHeaderStyle = createDateHeaderStyle(workbook);
        CellStyle purpleStyle = createPurpleStyle(workbook); // 西北省区专用样式

        // 创建一个Sheet
        Sheet sheet = workbook.createSheet("销售计划统计");
        
        // 定义指标列名
        String[] metricHeaders = {"填写人数", "填写数量", "重点项目关联数量", "重点项目关联率"};
        
        // 获取所有日期，并排序
        List<String> dates = new ArrayList<>(data.keySet());
        Collections.sort(dates);
        
        // 准备部门结构数据
        List<DeptRowData> deptRows = buildDeptStructure(data);
        
        // 创建标题行
        int rowIndex = 0;
        
        // 第一行：日期标题（合并单元格）
        Row dateHeaderRow = sheet.createRow(rowIndex++);
        Cell deptCell = dateHeaderRow.createCell(0);
        deptCell.setCellValue("部门");
        deptCell.setCellStyle(headerStyle);
        
        Cell sumPersonCell = dateHeaderRow.createCell(1);
        sumPersonCell.setCellValue("在编人数");
        sumPersonCell.setCellStyle(headerStyle);
        
        int colIndex = 2;
        for (String date : dates) {
            Cell dateCell = dateHeaderRow.createCell(colIndex);
            dateCell.setCellValue(date);
            dateCell.setCellStyle(dateHeaderStyle);
            // 合并日期单元格（合并该日期的4个指标列）
            sheet.addMergedRegion(new CellRangeAddress(0, 0, colIndex, colIndex + 3));
            colIndex += 4;
        }
        
        // 第二行：指标标题
        Row metricHeaderRow = sheet.createRow(rowIndex++);
        metricHeaderRow.createCell(0).setCellValue(""); // 部门列
        metricHeaderRow.getCell(0).setCellStyle(headerStyle);
        metricHeaderRow.createCell(1).setCellValue(""); // 在编人数列
        metricHeaderRow.getCell(1).setCellStyle(headerStyle);
        
        colIndex = 2;
        for (int i = 0; i < dates.size(); i++) {
            for (String metric : metricHeaders) {
                Cell cell = metricHeaderRow.createCell(colIndex++);
                cell.setCellValue(metric);
                cell.setCellStyle(headerStyle);
            }
        }
        
        // 填充数据行
        for (DeptRowData deptRow : deptRows) {
            Row row = sheet.createRow(rowIndex++);
            
            // 判断是否为特定省区，如果是则使用紫色背景样式
            boolean isSpecialRegion = deptRow.deptName != null && 
                (deptRow.deptName.contains("西北省区") || 
                 deptRow.deptName.contains("山东省区") || 
                 deptRow.deptName.contains("浙江省区"));
            CellStyle currentStyle = isSpecialRegion ? purpleStyle : dataStyle;
            CellStyle currentSummaryStyle = isSpecialRegion ? purpleStyle : summaryStyle;
            
            // 部门名称
            Cell deptNameCell = row.createCell(0);
            deptNameCell.setCellValue(deptRow.deptName);
            if (deptRow.isParent) {
                deptNameCell.setCellStyle(isSpecialRegion ? purpleStyle : summaryStyle);
            } else {
                deptNameCell.setCellStyle(currentStyle);
            }
            
            // 在编人数
            Cell sumPersonDataCell = row.createCell(1);
            sumPersonDataCell.setCellValue(deptRow.sumPerson);
            if (deptRow.isParent) {
                sumPersonDataCell.setCellStyle(isSpecialRegion ? purpleStyle : summaryStyle);
            } else {
                sumPersonDataCell.setCellStyle(currentStyle);
            }
            
            // 填充每个日期的数据
            colIndex = 2;
            for (String date : dates) {
                ImportExcelDTO deptData = deptRow.deptDataMap.get(date);
                
                if (deptData != null) {
                    // 填写人数
                    Cell cell1 = row.createCell(colIndex++);
                    cell1.setCellValue(deptData.getFollowPerson());
                    cell1.setCellStyle(deptRow.isParent ? currentSummaryStyle : currentStyle);
                    
                    // 填写数量
                    Cell cell2 = row.createCell(colIndex++);
                    cell2.setCellValue(deptData.getFollowCount());
                    cell2.setCellStyle(deptRow.isParent ? currentSummaryStyle : currentStyle);
                    
                    // 重点项目关联数量
                    Cell cell3 = row.createCell(colIndex++);
                    cell3.setCellValue(deptData.getRelaCount());
                    cell3.setCellStyle(deptRow.isParent ? currentSummaryStyle : currentStyle);
                    
                    // 重点项目相关率
                    Cell cell4 = row.createCell(colIndex++);
                    double rate = deptData.getRelaCal() != null ? deptData.getRelaCal() : 0;
                    cell4.setCellValue(rate / 100.0);
                    cell4.setCellStyle(deptRow.isParent ? currentSummaryStyle : currentStyle);
                    
                    // 设置百分比格式
                    CellStyle percentStyle = workbook.createCellStyle();
                    if (deptRow.isParent) {
                        Font font = workbook.createFont();
                        font.setBold(true);
                        percentStyle.setFont(font);
                        percentStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
                        percentStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    }
                    // 如果是特殊省区，设置紫色背景
                    if (isSpecialRegion) {
                        percentStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
                        percentStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    }
                    percentStyle.setAlignment(HorizontalAlignment.CENTER);
                    percentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                    percentStyle.setBorderBottom(BorderStyle.THIN);
                    percentStyle.setBorderTop(BorderStyle.THIN);
                    percentStyle.setBorderRight(BorderStyle.THIN);
                    percentStyle.setBorderLeft(BorderStyle.THIN);
                    percentStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("0.0%"));
                    cell4.setCellStyle(percentStyle);
                } else {
                    // 如果没有数据，填充空单元格
                    for (int i = 0; i < 4; i++) {
                        Cell cell = row.createCell(colIndex++);
                        cell.setCellValue("");
                        cell.setCellStyle(currentStyle);
                    }
                }
            }
        }
        
        // 添加总计行
        Row totalRow = sheet.createRow(rowIndex++);
        CellStyle totalStyle = createTotalStyle(workbook);
        
        // 总计行第一列：显示"总计"
        Cell totalLabelCell = totalRow.createCell(0);
        totalLabelCell.setCellValue("总计");
        totalLabelCell.setCellStyle(totalStyle);
        
        // 统计在编人数总计（只统计子部门，不统计父部门汇总行）
        int totalSumPerson = 0;
        for (DeptRowData deptRow : deptRows) {
            if (!deptRow.isParent) {
                if (deptRow.sumPerson != null) {
                    totalSumPerson += deptRow.sumPerson;
                }
            }
        }
        Cell totalSumPersonCell = totalRow.createCell(1);
        totalSumPersonCell.setCellValue(totalSumPerson);
        totalSumPersonCell.setCellStyle(totalStyle);
        
        // 统计每个日期的数据总计
        colIndex = 2;
        for (String date : dates) {
            int totalFollowPerson = 0;
            int totalFollowCount = 0;
            int totalRelaCount = 0;
            
            // 汇总所有子部门的数据（不统计父部门汇总行）
            for (DeptRowData deptRow : deptRows) {
                if (!deptRow.isParent) {
                    ImportExcelDTO deptData = deptRow.deptDataMap.get(date);
                    if (deptData != null) {
                        if (deptData.getFollowPerson() != null) {
                            totalFollowPerson += deptData.getFollowPerson();
                        }
                        if (deptData.getFollowCount() != null) {
                            totalFollowCount += deptData.getFollowCount();
                        }
                        if (deptData.getRelaCount() != null) {
                            totalRelaCount += deptData.getRelaCount();
                        }
                    }
                }
            }
            
            // 填写人数
            Cell cell1 = totalRow.createCell(colIndex++);
            cell1.setCellValue(totalFollowPerson);
            cell1.setCellStyle(totalStyle);
            
            // 填写数量
            Cell cell2 = totalRow.createCell(colIndex++);
            cell2.setCellValue(totalFollowCount);
            cell2.setCellStyle(totalStyle);
            
            // 重点项目关联数量
            Cell cell3 = totalRow.createCell(colIndex++);
            cell3.setCellValue(totalRelaCount);
            cell3.setCellStyle(totalStyle);
            
            // 重点项目关联率（重新计算：关联数量 / 填写数量）
            Cell cell4 = totalRow.createCell(colIndex++);
            double totalRate = 0;
            if (totalFollowCount > 0) {
                totalRate = (double) totalRelaCount / totalFollowCount;
            }
            cell4.setCellValue(totalRate);
            
            // 设置百分比格式
            CellStyle totalPercentStyle = workbook.createCellStyle();
            Font font = workbook.createFont();
            font.setBold(true);
            font.setFontHeightInPoints((short) 12);
            totalPercentStyle.setFont(font);
            totalPercentStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            totalPercentStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            totalPercentStyle.setAlignment(HorizontalAlignment.CENTER);
            totalPercentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            totalPercentStyle.setBorderBottom(BorderStyle.THIN);
            totalPercentStyle.setBorderTop(BorderStyle.THIN);
            totalPercentStyle.setBorderRight(BorderStyle.THIN);
            totalPercentStyle.setBorderLeft(BorderStyle.THIN);
            totalPercentStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("0.0%"));
            cell4.setCellStyle(totalPercentStyle);
        }
        
        // 自动调整列宽
        for (int i = 0; i < 2 + dates.size() * 4; i++) {
            sheet.autoSizeColumn(i);
        }
        
        // 为"重点项目关联数量"和"重点项目相关率"列设置更宽的宽度
        for (int i = 0; i < dates.size(); i++) {
            // 重点项目关联数量列索引：2 + i*4 + 2
            int index1 = 2 + i * 4 + 2;
            // 重点项目相关率列索引：2 + i*4 + 3
            int index2 = 2 + i * 4 + 3;
            
            sheet.setColumnWidth(index1, Math.max(sheet.getColumnWidth(index1), 6000)); // 45个字符宽度
            sheet.setColumnWidth(index2, Math.max(sheet.getColumnWidth(index2), 5000)); // 37个字符宽度
        }

        return workbook;
    }
    
    /**
     * 部门行数据结构
     */
    private static class DeptRowData {
        String deptName;
        Integer sumPerson;
        boolean isParent;
        Map<String, ImportExcelDTO> deptDataMap = new HashMap<>();
        
        DeptRowData(String deptName, Integer sumPerson, boolean isParent) {
            this.deptName = deptName;
            this.sumPerson = sumPerson;
            this.isParent = isParent;
        }
    }
    
    /**
     * 构建部门结构
     */
    private List<DeptRowData> buildDeptStructure(Map<String, List<ImportExcelDTO>> data) {
        List<DeptRowData> result = new ArrayList<>();
        
        // 获取第一个日期的数据作为基准
        List<ImportExcelDTO> sampleData = data.values().iterator().next();
        
        // 按部门分组
        Map<String, ImportExcelDTO> deptMap = new HashMap<>();
        for (ImportExcelDTO dto : sampleData) {
            deptMap.put(dto.getDeptName(), dto);
        }
        
        // 按部门前缀分组，找出所有的"区"（如营销一区、营销二区等）
        Map<String, List<String>> parentDeptMap = new HashMap<>();
        
        for (ImportExcelDTO dto : sampleData) {
            String deptName = dto.getDeptName();
            // 提取前缀，例如 "营销一区/北区" -> "营销一区"
            String parentPrefix = extractParentDeptPrefix(deptName);
            
            if (!parentDeptMap.containsKey(parentPrefix)) {
                parentDeptMap.put(parentPrefix, new ArrayList<>());
            }
            parentDeptMap.get(parentPrefix).add(deptName);
        }
        
        // 按父部门名称排序
        List<String> parentDeptNames = new ArrayList<>(parentDeptMap.keySet());
        // 自定义排序：营销一区、营销二区、营销三区、营销中心/公卫部、营销中心/渠道拓展部
        Map<String, Integer> orderMap = new HashMap<>();
        orderMap.put("营销一区", 0);
        orderMap.put("营销二区", 1);
        orderMap.put("营销三区", 2);
        orderMap.put("营销中心/公卫部", 3);
        orderMap.put("营销中心/渠道拓展部", 4);

        Collections.sort(parentDeptNames, (a, b) -> {
            Integer orderA = orderMap.get(a);
            Integer orderB = orderMap.get(b);
            
            if (orderA != null && orderB != null) {
                return orderA.compareTo(orderB);
            } else if (orderA != null) {
                return -1;
            } else if (orderB != null) {
                return 1;
            } else {
                return a.compareTo(b);
            }
        });
        
        // 为每个父部门创建数据
        for (String parentDept : parentDeptNames) {
            List<String> subDepts = parentDeptMap.get(parentDept);
            
            // 计算父部门的总人数（所有子部门人数之和）
            int parentSumPerson = 0;
            for (String dept : subDepts) {
                ImportExcelDTO subDto = deptMap.get(dept);
                if (subDto != null && subDto.getSumPerson() != null) {
                    parentSumPerson += subDto.getSumPerson();
                }
            }
            
            // 创建父部门行
            DeptRowData parentRow = new DeptRowData(parentDept, parentSumPerson, true);
            
            // 为父部门填充每个日期的数据（汇总子部门的数据）
            for (Map.Entry<String, List<ImportExcelDTO>> entry : data.entrySet()) {
                String date = entry.getKey();
                List<ImportExcelDTO> dateData = entry.getValue();
                
                // 汇总所有子部门的数据
                ImportExcelDTO summaryDto = new ImportExcelDTO();
                summaryDto.setDeptName(parentDept);
                summaryDto.setSumPerson(parentSumPerson);
                
                int totalFollowPerson = 0;
                int totalFollowCount = 0;
                int totalRelaCount = 0;
                
                // 遍历该日期下的所有数据，汇总子部门
                for (ImportExcelDTO dto : dateData) {
                    if (subDepts.contains(dto.getDeptName())) {
                        // 这是子部门，需要汇总
                        if (dto.getFollowPerson() != null) {
                            totalFollowPerson += dto.getFollowPerson();
                        }
                        if (dto.getFollowCount() != null) {
                            totalFollowCount += dto.getFollowCount();
                        }
                        if (dto.getRelaCount() != null) {
                            totalRelaCount += dto.getRelaCount();
                        }
                    }
                }
                
                summaryDto.setFollowPerson(totalFollowPerson);
                summaryDto.setFollowCount(totalFollowCount);
                summaryDto.setRelaCount(totalRelaCount);
                
                // 计算相关率：关联数量 / 填写数量 * 100
                if (totalFollowCount > 0) {
                    summaryDto.setRelaCal((totalRelaCount * 100) / totalFollowCount);
                } else {
                    summaryDto.setRelaCal(0);
                }
                
                parentRow.deptDataMap.put(date, summaryDto);
            }
            result.add(parentRow);
            
            // 添加子部门
            Collections.sort(subDepts);
            for (String dept : subDepts) {
                ImportExcelDTO subDto = deptMap.get(dept);
                if (subDto != null) {
                    DeptRowData subRow = new DeptRowData(dept, subDto.getSumPerson(), false);
                    
                    // 为子部门填充每个日期的数据
                    for (Map.Entry<String, List<ImportExcelDTO>> entry : data.entrySet()) {
                        String date = entry.getKey();
                        List<ImportExcelDTO> dateData = entry.getValue();
                        
                        ImportExcelDTO deptDateDto = null;
                        for (ImportExcelDTO dto : dateData) {
                            if (dto.getDeptName().equals(dept)) {
                                deptDateDto = dto;
                                break;
                            }
                        }
                        subRow.deptDataMap.put(date, deptDateDto);
                    }
                    result.add(subRow);
                }
            }
        }
        
        return result;
    }
    
    /**
     * 提取父部门前缀
     * 例如: "营销一区/北区" -> "营销一区"
     *       "营销二区/南区" -> "营销二区"
     *       "营销中心/公卫部" -> "营销中心/公卫部" (营销中心下的子部门作为独立组)
     *       "营销中心/渠道拓展部" -> "营销中心/渠道拓展部" (营销中心下的子部门作为独立组)
     */
    private String extractParentDeptPrefix(String deptName) {
        if (deptName == null) {
            return "";
        }
        
        // 查找第一个"/"或"-"的位置
        int slashIndex = deptName.indexOf('/');
        int dashIndex = deptName.indexOf('-');
        
        int separatorIndex = -1;
        if (slashIndex != -1 && dashIndex != -1) {
            separatorIndex = Math.min(slashIndex, dashIndex);
        } else if (slashIndex != -1) {
            separatorIndex = slashIndex;
        } else if (dashIndex != -1) {
            separatorIndex = dashIndex;
        }
        
        if (separatorIndex > 0) {
            // 提取第一部分作为前缀
            String prefix = deptName.substring(0, separatorIndex);
            
            // 特殊处理：如果前缀是"营销中心"，则保留完整部门名（按子部门独立分组）
            if (prefix.contains("营销中心")) {
                return deptName; // 返回完整部门名，让每个部门独立分组
            }
            
            return prefix;
        } else {
            // 如果没有分隔符，判断是否包含"区"或"中心"
            if (deptName.contains("区")) {
                // 提取到"区"的位置
                int quIndex = deptName.indexOf("区");
                return deptName.substring(0, quIndex + 1);
            } else if (deptName.contains("中心")) {
                // 提取到"中心"的位置
                int centerIndex = deptName.indexOf("中心");
                return deptName.substring(0, centerIndex + 2);
            }
            return deptName;
        }
    }
    
    /**
     * 创建汇总行样式（父部门样式）
     */
    private CellStyle createSummaryStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 12);
        style.setFont(font);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        return style;
    }
    
    /**
     * 创建日期标题样式
     */
    private CellStyle createDateHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 12);
        style.setFont(font);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        return style;
    }

    /**
     * 创建表头样式
     */
    private CellStyle createHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 12);
        style.setFont(font);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        return style;
    }

    /**
     * 创建数据样式
     */
    private CellStyle createDataStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        return style;
    }

    /**
     * 创建紫色背景样式（用于西北省区）
     */
    private CellStyle createPurpleStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        return style;
    }
    
    /**
     * 创建总计行样式
     */
    private CellStyle createTotalStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 12);
        style.setFont(font);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        return style;
    }
}

